<?php
/*=============================================================================
|| ##################################################################
||	phpFoX Konsort
|| ##################################################################
||
||	Copyright		: (C) 2005-2007 PHPFOX Limited a Reality Fox Creation
||	Contact			: info@phpfox.com
||
||	- phpFoX and all of its source code and files are protected by Copyright Laws.
||
||	- The license for phpFoX permits you to install this software on a single domain (.com, .org, .net, etc.).
||
||	- You may also not remove this copyright screen which shows the copyright information and credits for phpFoX (RealityFOX Creations).
||
||	- phpFoX Konsort is NOT a FREE software - http://www.phpfox.com/license/
||
|| ##################################################################
=============================================================================*/

/* $File: ./include/modules/Site/classes/PhpFox_ServiceStyle.class.php, $Author:  $ */



/** Implements functionality for working with styles
 *
 */
class PhpFox_Mod_Site_ServiceStyle extends BaseService
{
    /** Returns array with available style directories in DB
     * @return array
     */
    function getStyleNames()
    {
        $oCssItem = &$this->_oModule->getItem('Css');
        /* @var $oCssItem PhpFox_Mod_Site_ItemCss */

        list($aItems, $iCnt) = $oCssItem->getList();
        if (!$iCnt)
        {
            return false;
        }
        $aStyles = array();
        foreach ($aItems as $aItem)
        {
            $aStyles[] = $aItem['file'];
        }
        return $aStyles;
    }

    /** Checks, that style exists in database
     * @param string $sName style name
     * @param string $sDir style dir name
     * @return boolean
     */
    function styleExists($sName, $sDir)
    {
        $oCssItem = &$this->_oModule->getItem('Css');
        /* @var $oCssItem PhpFox_Mod_Site_ItemCss */
        $sName = Database::escape($sName);
        $sDir = Database::escape($sDir);
        $bItem = $oCssItem->oDb->getField('SELECT COUNT(*) '.
                                         ' FROM '.App::getT('css').' '.
                                         ' WHERE file = "'.$sDir.'" '.
                                         '   AND name = "'.$sName.'"');
        return $bItem;
    }

    /** Get current user style
     * @return string
     */
    function getUserStyle()
    {
        $oSecurityService = &App::getModuleService('Account', 'Security');
        /* @var $oSecurityService PhpFox_Mod_Account_ServiceSecurity */
        $oDb = &Database::get();

        if ( App::getParam('check_css_change') && !isset($_SESSION['update_style']) )
		{
			$_SESSION['update_style'] = time();
		}

		if ( App::getParam('check_css_change') && isset($_SESSION['update_style']) )
		{
			if ( floor((time()-$_SESSION['update_style'])/60) > 1 )
			{
				$oDb->update(App::getT('sys_sett'), array('val' => ''), "`code` = 'check_css_change'");
				App::deleteSetting();
				unset($_SESSION['update_style']);
				setcookie("cssName","","-1","/");
				setcookie("cssFile","","-1","/");
				setcookie("cssLayout","","-1","/");
			}
		}

   		if ( !App::getParam('check_css_change') && isset($_COOKIE['cssName']) && isset($_COOKIE['cssFile']) && file_exists(App::getParam('path') . 'file/style/' . $_COOKIE['cssFile'] . '/style.css') )
   		{
   			return array(strip_tags($_COOKIE['cssFile']),strip_tags($_COOKIE['cssName']), strip_tags($_COOKIE['cssLayout']));
   		}

   		static $aRow;

   		if (isset($aRow))
    	{
    		return array($aRow['file'], $aRow['name'], $aRow['cssLayout']);
    	}

        $oUser = &$oSecurityService->getCurrentUser();
        $aRow = array();
        if ($oUser && $oUser->aData['user'])
        {
            if ($iStyle = $oUser->aData['css'])
            {
                $aRow = $this->oDatabase->getRow("SELECT c.name, c.file, l.file as cssLayout ".
                	" FROM ". App::getT('css') ." AS c ".
                	" ,". App::getT('html_layout') ." AS l ".
                	" WHERE c.id = '". $iStyle ."' AND c.html = l.id");
            }
        }
        if (!$aRow)
        {
                $aRow = $this->oDatabase->getRow("SELECT c.name, c.file, l.file as cssLayout ".
                	" FROM ". App::getT('css') ." AS c ".
                	" ,". App::getT('html_layout') ." AS l ".
                	" WHERE c.default = '1' AND c.html = l.id");
        }

        if ( empty($aRow) )
        {
        	exit('Can\'t load CSS layout');
        }

		if ( !App::getParam('check_css_change') )
		{
			setcookie("cssName",$aRow['name'], 0,"/");
			setcookie("cssFile",$aRow['file'], 0,"/");
			setcookie("cssLayout",$aRow['cssLayout'], 0,"/");
		}

        return array($aRow['file'], $aRow['name'], $aRow['cssLayout']);
    }

    function getListOfThemes()
    {
   	 	$oCache = &App::newObject("Cache");
   	 	$oCache->setFile("css.php");
   	 	if ( $oCache->isCached() )
   	 	{
   	 	 	$oCache->doRequire();
   	 	 	$aRows = $oCache->details;
   	 	}
   		else
   		{
	    	$aRows = $this->oDatabase->getRows("SELECT id, name, file FROM ". App::getT('css') ."");
			$content = "<?php\n";
			foreach($aRows as $aRow => $value)
			{
	   			$content .= '$this->details[\''. $aRow .'\'] = array("id" => "'. $oCache->cleanFile($value['id']) .'","name" => "'. $oCache->cleanFile($value['name']) .'","file" => "'. $oCache->cleanFile($value['file']) .'");'."\n";
			}
			$content .= "?>";
			$oCache->createCached($content);
	    }
    	return $aRows;
    }

    function getThemeList()
    {
        $oCssItem = &$this->_oModule->getItem('Css');
        /* @var $oCssItem PhpFox_Mod_Site_ItemCss */

        $aThemeList = $oCssItem->oDb->getRows('SELECT COUNT(u.id) AS cnt, cs.file AS file, cs.name AS name, cs.default AS def '.
                                             ' FROM '.App::getT('css').' AS cs '.
                                             ' LEFT JOIN '.App::getT('user').' AS u ON cs.id = u.css '.
                                             ' GROUP BY cs.id');
        $iNoTheme = $oCssItem->oDb->getRows('SELECT COUNT(*) as notheme FROM ' . App::getT('user') . ' WHERE css = ""');

        return array($aThemeList, $iNoTheme[0]['notheme']);
    }

    function replaceStyleClass($sProperties, $sColor, $sTemplate)
    {
        $sOut = '';
        if ($sColor)
        {
            $sOut = str_replace('<%>', $sColor, $sTemplate);
        }
        if ($sProperties)
        {
            $sOut = $sProperties.' '.$sOut;
        }
        return $sOut;
    }
}
?>